/**
 * Copyright 2007 American Well Systems
 * All rights reserved.
 *
 * It is illegal to use, reproduce or distribute
 * any part of this Intellectual Property without
 * prior written authorization from American Well.
 */
package org.rich.charlesmurphy.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.rich.charlesmurphy.Properties;
import org.rich.charlesmurphy.core.DataLoaderCommand;
import org.rich.charlesmurphy.core.InputFileType;
import org.springframework.util.StringUtils;

/**
 * Extension of file chooser that adds inputs for the input file type and a hostname.
 * @author Rich O'Connell
 */
class CharliesFileChooser extends JFileChooser {
	private static final long serialVersionUID = 5967954663223945014L;

	private ButtonGroup grpFileType;
	private JTextField txtHost;
	private JTextField txtName;
	private JCheckBox chkFilterAjax;
	private InputFileType defaultFileType;
	private static String lastFileLoc = null;

	/**
	 * Constructo.
	 */
	public CharliesFileChooser(InputFileType defaultFileType) {
		super(determineFileLoc());
		this.defaultFileType = defaultFileType;
		setSize(700, 550);
	}

	private static String determineFileLoc() {
		return lastFileLoc != null ? lastFileLoc : Properties.getFileOpenLocation();
	}

	public void updateFileLoc() {
		lastFileLoc = getSelectedFile().getAbsolutePath();
	}

	@Override
	protected JDialog createDialog(Component parent)
			throws HeadlessException {
		JDialog dialog = super.createDialog(parent);

		JPanel topSection = new JPanel();
		topSection.setLayout(new BoxLayout(topSection, BoxLayout.Y_AXIS));
		JPanel p = new JPanel(new FlowLayout());

		p.add(new JLabel("Input file type:"));
		ButtonGroup bg = new ButtonGroup();
		for (InputFileType ift : InputFileType.values()) {
			JRadioButton rb = new JRadioButton(ift.getDisplayName(), ift == defaultFileType);
			rb.getModel().setActionCommand(ift.name());
			p.add(rb);
			bg.add(rb);
		}
		grpFileType = bg;
		topSection.add(p);

		p = new JPanel(new FlowLayout());
		p.add(new JLabel("Host:"));
		txtHost = new JTextField(14);
		p.add(txtHost);
		topSection.add(p);
		p = new JPanel(new FlowLayout());
		p.add(new JLabel("Session Name:"));
		txtName = new JTextField(14);
		p.add(txtName);
		topSection.add(p);
		p = new JPanel(new FlowLayout());
		p.add(new JLabel("Filter Ajax:"));
		p.add(chkFilterAjax = new JCheckBox());
		topSection.add(p);

		dialog.getContentPane().add(topSection, BorderLayout.NORTH);

		return dialog;
	}
	
	public DataLoaderCommand makeCommand() {
		String sessionName = getSessionname();
		File[] files = getSelectedFiles();
		if (!StringUtils.hasText(sessionName)) {
			if (files.length > 1) {
				//assume directory, use directory name
				sessionName = files[0].getParentFile().getName();
			}
			else {
				sessionName = files[0].getName();
			}
		}
		return new DataLoaderCommand(files, getFileType(),
				sessionName, getHostname(), getFilterAjax());
	}

	/**
	 * Get the host name entered by the user.
	 * @return
	 */
	private String getHostname() {
		return txtHost.getText();
	}

	/**
	 * Get the {@link InputFileType} entered by the user.
	 * @return
	 */
	private InputFileType getFileType() {
		return InputFileType.valueOf(grpFileType.getSelection().getActionCommand());
	}

	private String getSessionname() {
		return txtName.getText();
	}
	
	private boolean getFilterAjax() {
		return chkFilterAjax.getModel().isSelected();
	}
}